local MUD_HOLE		=	392
local FRAGILE_ICE	=	7200
local ICE_FISHHOLE	=	7236

local duration = 5 * 60000 -- 5 minutes

--- local crystal_table = {[8633] = {transform = 8637, add = 8306, chance = 2}, [8634] = {transform = 8638, add = 9743, chance = 2}, [8635] = {transform = 8639, add = 9980, chance = 2}} --- , [8636] = {transform = 8640, add = XXXXX, chance = 2}

local STONE_CONFIG = {
	stone_rock = {4471, 4472, 4473, 4474, 4475, 4476},
	stoneRockAid = 101,
	stoneHit = 5,
	stoneId = {1293, 1295},
	
	brokenPickMsg = "You broke pick.",
	effect = CONST_ME_BLOCKHIT,

	monsterChance = 5, --- in percent
	monsterName = "Stone Golem"
}

local IRON_CONFIG = {
	time_ = 6*60*60, --- seconds
	effect = CONST_ME_BLOCKHIT,
	iron_rock_table = {[5619] = {count = {2, 5}, newId = 5747}, [5620] = {count = {2, 5}, newId = 5750}, [5621] = {count = {1, 3}, newId = 5751}, [5622] = {count = {1, 3}, newId = 5752}, [5623] = {count = {1, 3}, newId = 5753}, [5624] = {count = {1, 3}, newId = 5754}} -- bez iron ore: (5747 - 5754) + (5866 - 5868) --- 5748, 5749
}

local EXHAUST = {
	STORAGE = 1,
	TIME = 4,
	EFFECT = CONST_ME_POFF,
	MSG = "You are exhausted."
}

local function transformBackIronOre(ID, newId, pos)
	local thing = getTileItemById(pos, newId)
	doTransformItem(thing.uid, ID)
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
	if (itemEx.actionid == 100) then
		if isInArray({354, 355}, itemEx.itemid) then
			doTransformItem(itemEx.uid, 392)
			doDecayItem(itemEx.uid)
			doSendMagicEffect(toPosition, CONST_ME_POFF)
			return true
		elseif itemEx.itemid == 4534 then
			doTransformItem(itemEx.uid, 469)
			doDecayItem(itemEx.uid)
			doSendMagicEffect(toPosition, CONST_ME_POFF)
			return true
		end
		return false
	end

	if(itemEx.itemid == FRAGILE_ICE) then
		doTransformItem(itemEx.uid, ICE_FISHHOLE)
		doSendMagicEffect(toPosition, CONST_ME_BLOCKHIT)
		return true
	end
	
	local IRON_ORE = IRON_CONFIG.iron_rock_table[itemEx.itemid]

	if IRON_ORE ~= nil then
		if getPlayerStorageValue(cid, EXHAUST.STORAGE) > os.time() then
			doSendMagicEffect(getCreaturePosition(cid), EXHAUST.EFFECT)
			return doPlayerSendCancel(cid, EXHAUST.MSG)
		end
		
		doSendMagicEffect(toPosition, IRON_CONFIG.effect)
		doCreatureSay(cid, "Stuk!", TALKTYPE_ORANGE_1)
		doPlayerAddItem(cid, 5880, math.random(IRON_ORE.count[1], IRON_ORE.count[2]), true) --- canDropOnMap = true
		-- doCreateItem(5880, math.random(IRON_ORE.count[1], IRON_ORE.count[2]), toPosition)
		doTransformItem(itemEx.uid, IRON_ORE.newId)
		addEvent(transformBackIronOre, IRON_CONFIG.time_*1000, itemEx.itemid, IRON_ORE.newId, {x = toPosition.x, y = toPosition.y, z = toPosition.z}) 
		setPlayerStorageValue(cid, EXHAUST.STORAGE, os.time() +EXHAUST.TIME)
		
	elseif isInArray(STONE_CONFIG.stone_rock, itemEx.itemid) and itemEx.actionid == STONE_CONFIG.stoneRockAid then
		if getPlayerStorageValue(cid, EXHAUST.STORAGE) > os.time() then --- EXHAUST
			doSendMagicEffect(getCreaturePosition(cid), EXHAUST.EFFECT)
			return doPlayerSendCancel(cid, EXHAUST.MSG)
		end
		
		if math.random(1, 1000) == 1 then
			doRemoveItem(item.uid)
			doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, STONE_CONFIG.brokenPickMsg)
		end
		
		if STONE_CONFIG.stoneHit > 0 then
			doTargetCombatHealth(0, cid, COMBAT_PHYSICALDAMAGE, -STONE_CONFIG.stoneHit, -STONE_CONFIG.stoneHit, CONST_ME_NONE)
		end
		
		if math.random(1, 100) <= STONE_CONFIG.monsterChance then
			local monsterPosTable = {{x = toPosition.x -1, y = toPosition.y +1, z = toPosition.z}, {x = toPosition.x +1, y = toPosition.y +1, z = toPosition.z}}
			for _, mon in pairs(monsterPosTable) do
				doSummonCreature(STONE_CONFIG.monsterName, mon)
			end
		end
		doCreateItem(STONE_CONFIG.stoneId[math.random(1, 2)], 1, getCreaturePosition(cid))
		doSendMagicEffect(toPosition, STONE_CONFIG.effect)
		setPlayerStorageValue(cid, EXHAUST.STORAGE, os.time() +EXHAUST.TIME)
		doSendMagicEffect(getCreaturePosition(cid), EXHAUST.EFFECT)
		
		setPlayerStorageValue(cid, EXHAUST.STORAGE, os.time() +EXHAUST.TIME) --- EXHAUST
	end
	return false
end
